const mongoose = require("mongoose");
const SampleVillage = require("../models/sampleVillageModel");

//创建样本村
exports.createSampleVillage = async (req, res) => {
  try {
    req.body.group = req.user.group;
    const newSampleVillage = await SampleVillage.create(req.body);
    // 201 Created 已创建
    res.status(201).json({
      status: "success",
      data: newSampleVillage,
    });
  } catch (err) {
    console.log("创建样本村失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};

//删除样本村
exports.deleteSampleVillage = async (req, res) => {
  try {
    const id = mongoose.Types.ObjectId(req.params);
    const data = await SampleVillage.deleteOne({ _id: id });
    res.status(200).json({
      status: "success",
      data: data,
    });
  } catch (err) {
    console.log("删除样本村失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};

//修改样本村
exports.updateSampleVillage = async (req, res) => {
  try {
    const id = mongoose.Types.ObjectId(req.params);
    const newSampleVillage = await SampleVillage.findByIdAndUpdate(
      id,
      req.body,
      {
        new: true,
      }
    );
    if (!newSampleVillage) {
      return res.status(404).json({
        message: "修改的样本村并不存在",
      });
    }
    res.status(200).json({
      status: "success",
      data: newSampleVillage,
    });
  } catch (err) {
    console.log("修改样本村失败", err);
    res.status(404).json({
      err,
    });
  }
};

//获取指定分组全部样本村（管理员）
exports.getSampleVillageByGroup = async (req, res) => {
  try {
    const SampleVillageList = await SampleVillage.find({
      group: req.user.group,
    });
    res.status(200).json({
      status: "success",
      data: SampleVillageList,
    });
  } catch (err) {
    console.log("获取指定分组全部样本村失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};

//样本村发布（分组） unpublish 未发布  publishing 正在发布
exports.publishSampleVillageByGroup = async (req, res) => {
  try {
    const idGroup = [];
    req.body.map((item) => {
      idGroup.push(mongoose.Types.ObjectId(item));
    });

    // 将原来publishing变为unpublish  重置
    await SampleVillage.updateMany(
      { group: req.user.group, type: "publishing" },
      { $set: { type: "unpublish" } }
    );

    await SampleVillage.updateMany(
      { _id: { $in: idGroup } },
      { $set: { type: "publishing" } }
    );

    const newVillageList = await SampleVillage.find({ group: req.user.group });

    res.status(200).json({
      status: "success",
      data: newVillageList,
    });
  } catch (err) {
    console.log("样本村发布失败", err);
    res.status(404).json({
      err,
    });
  }
};

//获取指定分组已发布样本村（已发布）
exports.getPublishedSampleVillageByGroup = async (req, res) => {
  try {
    const sampleVillage = await SampleVillage.find({
      group: req.user.group,
      type: "publishing",
    });
    res.status(200).json({
      status: "success",
      data: sampleVillage,
    });
  } catch (err) {
    console.log("获取指定分组已发布样本村失败", err);
    res.status(404).json({
      Error: err,
    });
  }
};
